from typing import Union, List

from utils.cache_control import CacheHandler
from config.config import BASE_URL
from utils.others.models import TestCaseEnum


class CaseDataCheck:
    def __init__(self, data: dict):
        self.case_data = data


    # TODO 数据解析, 判断该条用例数据填写是否符合规范


    def get_url(self):
        # return BASE_URL + self.case_data.get()
        pass


    def to_json(self, key: str):
        if self.case_data.get(key) is not None:
            return eval(self.case_data.get(key))
        return None

class CaseData(CaseDataCheck):
    def case_process(self, case_id_switch: Union[bool, None] = None):
        """将数据格式化：URL拼接，及json字符串格式化"""
        # print(BASE_URL)
        # print(self.case_data.get(TestCaseEnum.PATH.value[0]))
        # print(BASE_URL + self.case_data.get(TestCaseEnum.PATH.value[0]))
        case_data = {
            'id': self.case_data.get(TestCaseEnum.ID.value[0]),
            'feature': self.case_data.get(TestCaseEnum.FEATURE.value[0]),
            'story': self.case_data.get(TestCaseEnum.STORY.value[0]),
            'title': self.case_data.get(TestCaseEnum.TITLE.value[0]),
            'url': BASE_URL + self.case_data.get(TestCaseEnum.PATH.value[0]),
            'method': self.case_data.get(TestCaseEnum.METHOD.value[0]),
            'headers': self.to_json(TestCaseEnum.HEADERS.value[0]),
            'params': self.to_json(TestCaseEnum.PARAMS.value[0]),
            'data': self.to_json(TestCaseEnum.DATA.value[0]),
            'json': self.to_json(TestCaseEnum.JSON.value[0]),
            'files':  self.case_data.get(TestCaseEnum.FILES.value[0]),
            'json_check': self.case_data.get(TestCaseEnum.JSON_CHECK.value[0]),
            'assert_type': self.case_data.get(TestCaseEnum.ASSERT_TYPE.value[0]),
            'expected': self.case_data.get(TestCaseEnum.EXPECTED.value[0]),
            'sql_check': self.case_data.get(TestCaseEnum.SQL_CHECK.value[0]),
            'sql_expected': self.case_data.get(TestCaseEnum.SQL_EXPECTED.value[0]),
            'json_extract': self.case_data.get(TestCaseEnum.JSON_EXTRACT.value[0]),
            'sql_extract': self.case_data.get(TestCaseEnum.SQL_EXTRACT.value[0]),
            'is_run': self.case_data.get(TestCaseEnum.IS_RUN.value[0]),
        }

        # print('case_data', case_data)
        return case_data


class GetTestCase:
    @staticmethod
    def case_data(case_id_lists: List):
        """根据id从缓存池提取对应的测试用例数据"""
        case_lists = []
        for id in case_id_lists:
            _data = CacheHandler.get_cache(id)
            case_lists.append(_data)
        # print('case_lists======', case_lists)
        return case_lists
